<?php
namespace app\wechat\home;

use app\common\controller\Wechat;

/**
 * 关注事件消息处理
 * @author King
 *
 */
class EventSubscribe extends Wechat
{

	/**
	 * 关注事件消息处理入口
	 *
	 * @author King
	 * @date 2020年8月13日
	 *
	 * @param \WeChat\Receive $wechat
	 * @param array $data
	 * @return mix
	 */
	public function index($wechat, $data) {
		$openid = $wechat->getOpenid();
		$upData = [
			'openid' => $openid,
			'update_time' => time(),
		];
		$info = db('wechatUser')->where('openid', $openid)->field('id')->find();
		if (!$info) {
			$upData['create_time'] = time();
			db('wechatUser')->insertGetId($upData);
		}

		$whchatUser = \We::WeChatUser(config('wechat.'));
		$data = $whchatUser->getUserInfo($openid);
		$upData = [
			'nickname' => $data['nickname'],
			'sex' => $data['sex'],
			'avatar' => $data['headimgurl'],
			'remark' => $data['remark'],
			'subscribe' => $data['subscribe'],
			'country' => $data['country'],
			'province' => $data['province'],
			'city' => $data['city'],
			'subscribe_time' => $data['subscribe_time'],
			'update_time' => time(),
		];
		db('wechatUser')->where('openid', $openid)->update($upData);

		$wechat->text('欢迎关注本公众号^_^')->reply();
	}
}